/**
 * @file    makeman.cpp
 * @brief   
 *
 *
 *
 * XSL Transformations, based on libxslt1.
 * Required Libraries (and headers):
 *           - apt-get install libxslt1-dev libxml
 * [ -lxslt ]
 *
 * @version $Id: makeman.cpp 1708 2010-01-26 22:28:01Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ******************************************************************************/

 
#include <string>

// from libxml2-dev	(g++ -lxml2)
#include <libxml/xmlIO.h>
#include <libxml/DOCBparser.h>

// from libxslt1-dev	(g++ -lxslt)
#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>

////#include <libxml/xinclude.h>
////#include <libxml/catalog.h>
////#include <libxslt/xslt.h>
////#include <libxml/xmlmemory.h>
////#include <libxml/debugXML.h>
////#include <libxml/HTMLtree.h>


using namespace std;

//////////////////////////////////////////////////////////////////
int process_xml_xslt( const std::string& source_xml, const std::string& stylesheet, const std::string& output );


int main(int argc, char **argv)
 {
	printf( "Starting..\n" );

	std::cout << "Enter full WOSH root path [/my/folder/wosh]:";
	std::cin >> wosh_path;
	if ( wosh_path.at(wosh_path.size()) != '/' )
		wosh_path += "/";

	std::string man_folder = wosh_path + "var/man/";
	std::string xsl_folder = wosh_path + "var/stylesheets/";

	std::string stylesheet = "manpage.xsl";

	std::string source_xml = wosh_path + "/src/bundles/services/Man/man.xml";

	printf( "Processing File..\n" );

	int ret = process_xml_xslt( source_xml, xsl_folder+stylesheet, "cron.html" );


	printf( "Completed.\n" );

	xsltCleanupGlobals();
	xmlCleanupParser();

	return 0;
 }

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

int process_xml_xslt( const std::string& source_xml, const std::string& stylesheet, const std::string& output )
 {
	xsltStylesheetPtr cur = NULL;
	xmlDocPtr doc, res;
	const char *params[16 + 1];
	int nbparams = 0;
	params[nbparams] = NULL;

	xmlSubstituteEntitiesDefault(1);

	cur = xsltParseStylesheetFile((const xmlChar *) stylesheet.c_str() );
	doc = xmlParseFile( source_xml.c_str()  );
	res = xsltApplyStylesheet(cur, doc, params);

	FILE * pFile;
	pFile = fopen( output.c_str(), "w" );
	if (pFile!=NULL) {
		xsltSaveResultToFile(pFile, res, cur);
		fclose (pFile);
	 }
	//else
		xsltSaveResultToFile(stdout, res, cur);

	xsltFreeStylesheet(cur);
	xmlFreeDoc(res);
	xmlFreeDoc(doc);

	return 0;
 }



























